/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/


#ifndef CORRELATIONPARENT_H
#define CORRELATIONPARENT_H


#include <iostream>
#include <fstream>

#include <opencv/cv.h>
#include <opencv/cvaux.h>
#include <opencv/highgui.h>
#include <opencv/cxcore.h>
#include <opencv/cvvidsurv.hpp>

#include "Image.h"


using namespace std;


// User-defined types
typedef struct RectDouble
{
    double x;
    double y;
    double width;
    double height;
}
RectDouble;


// Constants used for static array sizing
const int max_num_blobs = 1000;
const int max_neighbor_count = 36;  // Allow 10-degree increments
const int calculation_count = 3;


/*
  CorrelationParent class

  This class containing members and functions (all of which are abstract) is the parent of the Data, Correlation, and
  RunTest classes.

  Correlation class -  contains implementations of the member functions which perform tasks needed for the actual
    digital image correlation
  RunTest class - contains implementations of the functions which each performs a suite of tests on the corresponding
    function in the Correlation class
  Data class - contains implementations which load test data for each test suite to be performed

  */
class CorrelationParent {

public:

    /* ----- Class Implementation Functions ------------------------------------------------------------------------- */

    CorrelationParent();


    /* ----- Level 0 Functions -------------------------------------------------------------------------------------- */

    virtual void initObject() { return; }
    virtual bool analyzeImage(string destPathName, string sourcePathName, string fileName, bool makeReferenceImage) { return false; }
    virtual void storePositionField(string path, bool isFirstRun=false) { return; }
    virtual void storeStrainField(string path, bool isFirstRun=false) { return; }
    virtual void outputImage(bool showInWindow=true, bool switchIsDot=false) { return; }
    virtual void outputOverlay(bool showInWindow=true) { return; }


    /* ----- Level 1 Functions -------------------------------------------------------------------------------------- */

    virtual void loadPersistentValues() { return; }
    virtual void savePersistentValues() { return; }
    virtual bool inputImage(string file) { return false; }
    virtual void blobAnalysis() { return; }
    virtual void extractPositionField(bool isFirstRun=false) { return; }
    virtual void generateGridMap() { return; }
    virtual void computeStrain(bool showDebug) { return; }
    virtual void drawCurrentState(bool showInWindow=false, bool switchIsDot=false) { return; }


    /* ----- Level 2 Functions -------------------------------------------------------------------------------------- */

    virtual RgbImage subsetImage(RgbImage src) { RgbImage a; return a; }
    virtual void filterImage(IplImage *imgInLocal) { return; }
    virtual int matchBlob(int r, int c, int criteria) { return -1; }
    virtual void updateBoundingBox(int blob_track, int r, int c) { return; }
    virtual void mergeBlobs(int fromBlob, int toBlob) { return; }
    virtual void postProcess() { return; }
    virtual void blobProbability() { return; }
    virtual void identifyDots() { return; }
    virtual bool isValidPointSet(double pt[2][3], double pt_star[2][3], int p, int n1, int n2) { return false; }
    virtual void localStrain(double pt[2][3], double pt_star[2][3], int count, int method) { return; }
    virtual double minAbs(double a, double b) { return 0; }
    virtual double maxAbs(double a, double b) { return 0; }
    virtual double minIgnore(double a, double b) { return -1; }
    virtual double maxIgnore(double a, double b) { return -1; }
    virtual void drawBlobProperty(int whichProperty, int blob_track, int B, int G, int R) { return; }
    virtual void drawCurrentPixel(int r, int c, int B, int G, int R) { return; }
    virtual void debugHandle(int direction=-1, string functionName="unknown function") { return; }


public:


    string persistFileString;
    int enable_debug;
    bool enable_step;
    int image_sequence;
    int h, w;
    int colorSpace;
    int colorSpacePrev;
    CvRect ROI;
    RgbImage imgInFull;
    RgbImage imgIn;
    BwImage bw;
    RgbImage imgProc;
    int R_lower, R_upper, G_lower, G_upper, B_lower, B_upper;
    int H_lower, H_upper, S_lower, S_upper, V_lower, V_upper;
    int num_blobs;
    int found;
    double min_blob_area, max_blob_area;
    int blob_bound[3][max_num_blobs];
    int blob_area[max_num_blobs];
    double blob_eccentricity[max_num_blobs];
    double blob_centroid[2][max_num_blobs];
    double bounding_box[4][max_num_blobs];
    double major_axis[max_num_blobs];
    double minor_axis[max_num_blobs];
    double blob_probability[max_num_blobs];
    bool is_dot[max_num_blobs];
    int blob_dist_thresh;
    int area_thresh;
    int num_dots;
    double migration_dist_thresh;
    RectDouble pfRefRectangle;
    RectDouble pfRectangle;
    double positionField_ref[2][max_num_blobs];
    double positionField[2][max_num_blobs];
    double min_direction_angle;
    double max_direction_angle;
    double max_neighbor_dist;
    int gridMap[max_neighbor_count][max_num_blobs];
    double epsxx[max_num_blobs];
    double epsxy[max_num_blobs];
    double epsyx[max_num_blobs];
    double epsyy[max_num_blobs];
    double epsCollection[4][calculation_count];
    double globalMinStrain, globalMaxStrain;
    int t;
    double dotProbabilityThresh;

};


#endif // CORRELATIONPARENT_H
